﻿Namespace Muhasebe.OdemeEmri
	Module Functions

		Function Oku(Token As Token, eno As Integer) As Recordset
			Return Token.OpenSQL("select * from muhasebe..odemeemri where eno = @eno", eno)
		End Function

		Function Hesap(Token As Token, eno As Integer, borc As Boolean) As Recordset
			Q("select *,(select hesapadi from muhasebe..hesplan where hesapno = a.hesapno) as hesapadi")
			Q("from muhasebe..odemeemridetay a where eno = @eno and " & If(borc, "borc", "alacak") & " > 0")
			Return Token.OpenSQL("*", eno)
		End Function

		Function CekListesi(Token As Token, eno As Integer) As Recordset
			Q("select * from muhasebe..cek where odeemrino = @eno and yazdir = 1 order by cekno")
			Return Token.OpenSQL("*", eno)
		End Function

		Function CekListesi(Token As Token, d1 As Date, d2 As Date) As Recordset
			Q("select *,case when isnull(iptalkod,'') = 'I' then 'I' when boztar is not null then 'B' else ' ' end as durum")
			Q("from muhasebe..cek where tarih between @d1 and @d2 and yazdir = 1")
			Q("and odeemrino in (select eno from muhasebe..odemeemri where onay = 1)")
			Q("order by cekno")
			Return Token.OpenSQL("*", d1, d2)
		End Function

		Function CekListesiBozdurulmayan(Token As Token, d1 As Date, d2 As Date) As Recordset
			Q("select *,case when isnull(iptalkod,'') = 'I' then 'I' when boztar is not null then 'B' else ' ' end as durum")
			Q("from muhasebe..cek where tarih between @d1 and @d2 and yazdir = 1")
			Q("and odeemrino in (select eno from muhasebe..odemeemri where onay = 1)")
			Q("and isnull(boztar,'19760101') not between @d1 and @d2")
			Q("order by cekno")
			Return Token.OpenSQL("*", d1, d2)
		End Function

		Function HesapBakiyeleri(Token As Token, eno As Integer) As Recordset
			Q("select *,(select hesapadi from muhasebe..hesplan where hesapno = a.hesapno) as hesapadi")
			Q("from muhasebe..odemeemridetay a where eno = @eno and borc > 0 order by hesapno")
			Return Token.OpenSQL("*", eno)
		End Function

		Function BozdurulmayanCekler(Token As Token) As Recordset
			Return Token.OpenSQL("select * from muhasebe..cek where ceknoyeni is not null and boztar is null order by ceknoyeni")
		End Function

		Function CekOku(Token As Token, cekno As Integer) As Recordset
			Return Token.OpenSQL("select * from muhasebe.dbo.cek where ceknoyeni = @cekno", cekno)
		End Function

		Sub CekBozdur(Token As Token, boztar As Date, ceklistesi As String)
			Token.BeginTran()
			Token.ExecSQL("update muhasebe..cek set boztar = @boztar where ceknoyeni in (" & ceklistesi & ")", boztar)
			Token.CommitTran()
		End Sub

		Sub CekDurumuDegistir(Token As Token, ceknoyeni As Integer, durum As String, tarih As Date)
			Dim Rs = Muhasebe.OdemeEmri.CekOku(Token, ceknoyeni)
			Rs("iptalkod") = Choose(durum, "", "", "I")
			Rs("boztar") = If(durum = "1", DBNull.Value, tarih)
			Rs.Save()
		End Sub

		Sub CekIptal(Token As Token, ceknoyeni As Integer, iptaltar As Date)
			Token.BeginTran()
			Token.ExecSQL("update muhasebe..cek set iptalkod = 'I', boztar = @boztar where ceknoyeni = @ceknoyeni", iptaltar, ceknoyeni)
			Token.CommitTran()
		End Sub

		Sub CekNoKaydet(Token As Token, liste As String)
			Token.BeginTran()
			For Each Item As String In liste.Split(",")
				Dim Items() As String = Item.Split(";")
				Token.ExecSQL("update muhasebe..cek set ceknoyeni = @ceknoyeni where cekno = @cekno", Items(1), Items(0))
			Next
			Token.CommitTran()
		End Sub

		Function SonGirilenler(Token As Token) As Recordset
			Return Token.OpenSQL("select top 100 * from muhasebe..odemeemri order by eno desc")
		End Function

	End Module
End Namespace

